#include "sqlpatientmodel.h"
#include "../share/global.h"

SqlPatientModel::SqlPatientModel(QObject *parent) :
    QSqlQueryModel(parent)
{
}

QVariant SqlPatientModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (Qt::DisplayRole == role) {
        if (Qt::Horizontal == orientation) {
            switch (section) {
            case PatientId:
                return tr("Patient ID");
            case PatientName:
                return tr("Name");
            case PatientSex:
                return tr("Sex");
            case PatientBirth:
                return tr("Birthdate");
            case PatientAge:
                return tr("Age");

            case MedicalAlerts:
                return tr("Medical Alerts");
            case PatientSize:
                return tr("Size");
            case PatientWeight:
                return tr("Weight");
            case PatientAddr:
                return tr("Address");
            case PatientPhone:
                return tr("Phone");

            default:
                return QSqlQueryModel::headerData(section, orientation, role);
            }
        }
    }
    return QSqlQueryModel::headerData(section, orientation, role);
}

QVariant SqlPatientModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid() && (Qt::DisplayRole==role)) {
        switch (index.column()) {
        case PatientSex:
            return sex2trSex(QSqlQueryModel::data(index, role).toString());
        case PatientAge:
        {
            QString ageStr = QSqlQueryModel::data(index, role).toString();
            QString ageUnit = ageStr.right(1);
            if ((ageUnit=="Y") || (ageUnit=="y")) {
                return QString("%1%2").arg(ageStr.left(ageStr.size()-1), tr("Years"));
            } else if ((ageUnit=="M") || (ageUnit=="m")) {
                return QString("%1%2").arg(ageStr.left(ageStr.size()-1), tr("Months"));
            } else if ((ageUnit=="W") || (ageUnit=="w")) {
                return QString("%1%2").arg(ageStr.left(ageStr.size()-1), tr("Weeks"));
            } else if ((ageUnit=="D") || (ageUnit=="d")) {
                return QString("%1%2").arg(ageStr.left(ageStr.size()-1), tr("Days"));
            } else {
                return ageStr;
            }
        }
        default:
            return QSqlQueryModel::data(index, role);
        }
    }
    return QSqlQueryModel::data(index, role);
}
